package com.bwda.dsrs.base.centerbase;

import com.bwda.dsrs.base.centerbase.form.DealResult;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: yangjun
 * @create: 2019-11-01 11:06
 */
public class BaseContextHandler {
    public static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = new ThreadLocal<Map<String, Object>>();
    private static final String USER_CODE = "userCode";
    private static final String USER_NAME = "userName";
    private static final String ORG_ID = "orgId";
    private static final String ORG_NAME = "orgName";
    private static final String ORG_ADMIN_CODE = "orgAdminCode";
    private static final String HEADER_MAPS = "header_maps";
    private static final String IP_ADDR = "ip_addr";
    public static void set(String key, Object value) {
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map == null) {
            map = new HashMap<String, Object>(10);
            THREAD_LOCAL.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map == null) {
            map = new HashMap<String, Object>(10);
            THREAD_LOCAL.set(map);
        }
        return map.get(key);
    }


    public static String getUserCode() {
        Object value = get(USER_CODE);
        return returnObjectValue(value);
    }

    public static void setUserCode(String userCode) {
        set(USER_CODE, userCode);
    }

    public static String getUserName() {
        Object value = get(USER_NAME);
        return returnObjectValue(value);
    }

    public static void setUserName(String userName) {
        set(USER_NAME, userName);
    }

    public static String getOrgId() {
        Object value = get(ORG_ID);
        return returnObjectValue(value);
    }

    public static void setOrgId(String orgId) {
        set(ORG_ID, orgId);
    }

    public static String getOrgName() {
        Object value = get(ORG_NAME);
        return returnObjectValue(value);
    }

    public static void setOrgName(String orgName) {
        set(ORG_NAME, orgName);
    }

    public static String getOrgAdminCode() {
        Object value = get(ORG_ADMIN_CODE);
        return returnObjectValue(value);
    }

    public static void setOrgAdminCode(String orgAdminCode) {
        set(ORG_ADMIN_CODE, orgAdminCode);
    }

    public static Map<String, String> getHeaderMaps() {
        Object value = get(HEADER_MAPS);

        return value == null ? null : (Map) value;
    }

    public static void setHeaderMaps(Map<String, String> headerMaps) {
        set(HEADER_MAPS, headerMaps);
    }

    private static String returnObjectValue(Object value) {
        return value == null ? null : value.toString();
    }

    public static void remove() {
        THREAD_LOCAL.remove();
    }

    public static void setAuthorization(String accessToken) {
        set(BaseConstants.AUTHORIZATION, accessToken);
    }
    public static String getAuthorization() {
        Object value = get(BaseConstants.AUTHORIZATION);
        return returnObjectValue(value);
    }
    public static void setIpAddr(String ipAddr) {
        set(IP_ADDR, ipAddr);
    }
    public static String getIpAddr() {
        Object value = get(IP_ADDR);
        return returnObjectValue(value);
    }
    public static DealResult getDealResult(String key) {
        boolean test = false;
        //打印所有的map key
        if(test){
            Map<String, Object> map = THREAD_LOCAL.get();
            if(map!=null){
                for(Map.Entry<String, Object> entry : map.entrySet()){
                    String mapKey = entry.getKey();
                    DealResult mapValue = (DealResult)entry.getValue();
                    System.out.println(mapKey+":"+mapValue);
                }
            }


        }


        DealResult dealResult;
        if (get(key) == null) {
            dealResult = new DealResult();
        } else {
            dealResult = (DealResult) get(key);
        }
        return dealResult;
    }

}